<%@page import="com.ignou.mca.beans.SubjectBean"%>
<%@page import="java.util.Date"%>
<%@page import="com.ignou.mca.beans.ExamBean"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ignou.mca.datasource.DatabaseOperations"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.ignou.mca.beans.UserVO"%>
<%@page import="com.ignou.mca.beans.QuestionsBean"%><html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="theme/style.css"
	media="screen" />
<title>OES - Add Exam</title>
<script type="text/javascript">
	function deleteExamConfirm(examId)
	{
		var altTxt = "You are trying to delete "+examId+" exam.\n Click 'OK' to continue to delete. \n Click 'Cancel' to cancel the submission. '";
		if (confirm(altTxt))
		{
			window.location.href = 'DeleteExam.jsp?exam='+examId; 
		}
		else
		{
		}
	}
	
	function inputsValidations() 
	{
		var examName,noOfQues,maxMarks,passMarks,examFees,examDuration,subjectName;
		examName = document.fm.examName.value;
		noOfQues = document.fm.noOfQues.value;
		maxMarks = document.fm.maxMarks.value;
		passMarks = document.fm.passMarks.value;
		examFees = document.fm.examFees.value;
		examDuration = document.fm.examDuration.value;
		subjectName = document.fm.subjectID.value;

		var values = [];
		var cbs = document.forms['fm'].elements['quesions'];
		for(var i=0;i<cbs.length;i++){
		  if(cbs[i].checked){
		    values.push(cbs[i].value);
		  } 
		}
		document.fm.questionIdsSelected.value = values.join('| ');
		
		if (examName.length < 1) 
		{
			alert("You must enter Exam Name");
			document.fm.examName.focus();
			return false;
		} 
		else if(subjectName==0)
		{
			alert("You must select Subject Name"+subjectName);
			document.fm.subjectName.focus();
			return false;
		}
		else if (noOfQues.length < 1) 
		{
			alert("You must enter Number of Questions in the exam");
			document.fm.noOfQues.focus();
			return false;
		}
		else if (maxMarks.length < 1) 
		{
			alert("You must enter Maximum marks for the exam.");
			document.fm.maxMarks.focus();
			return false;
		}
		else if(passMarks.length<1)
		{
			alert("You must enter pass marks.");
			document.fm.passMarks.focus();
			return false;
		}else if(examFees.length<1)
		{
			alert("You must enter examination fees");
			document.fm.examFees.focus();
			return false;
		}
		else if(examDuration.length<1)
		{
			alert("You must enter duration of Exam.");
			document.fm.examDuration.focus();
			return false;
		}
		else if(noOfQues.length>1)
		{
			return isNumeric(noOfQues.value,document.fm.noOfQues);
		}
		else if(noOfQues!=values.length)
		{
			alert("You must select "+noOfQues+" questions. But you have selected "+values.length);
			return false;
		}
		else if(maxMarks.length>1)
		{
			return isNumeric(maxMarks.value,document.fm.maxMarks);
		}
		else if(examDuration.length>1)
		{
			return isNumeric(examDuration.value,document.fm.examDuration);
		}
		else if(passMarks.length>1)
		{
			return isNumeric(passMarks.value,document.fm.passMarks);
		}
		else if(examFees.length>1)
		{
			return isNumeric(examFees.value,document.fm.examFees);
		}
		else if(passMarks>maxMarks)
		{
			alert("Pass marks should be less than max marks");
			return false;
		}
		
		return true;
	}
	
	function isNumeric(value,field) 
	{
		try {
	        var inpVal = parseInt(value, 10);
	        if (isNaN(inpVal)) 
	        {
	            alert("Please enter a number only.");
	            field.focus();
	            return false;
	        }
	    } 
		catch (e) 
	    {
			return false;
	    }
		return true;
	}


	
	function changed()
	{
		var subName = document.fm.subjectName; 
		var subId = subName.options[subName.selectedIndex].value;
		document.fm.subjectID.value = subId;
	}

	function showQuestion(questionId)
	{
		newwindow=window.open("./ShowQuestion.jsp?question="+questionId+"",'Questions','height=400,width=480,resizable=0');
		if (window.focus) {newwindow.focus();}
	}

	function userFocus()
	{
		document.fm.examName.focus();
	}
	
</script>
</head>
<body onload="userFocus()">
	<div id="MainWrapper">
		<jsp:include page="header.jsp" /> 
		<jsp:include page="LeftContainer.jsp" />
		<div id="MainContent">
		<center><h2>Add Exam</h2></center>		
		<%
			UserVO uvo = (UserVO) session.getAttribute("UserVO");
			if(uvo==null)
			{
				response.sendRedirect("./Login.jsp");
			}
			else
			{
				String sub = request.getParameter("subject");
				int subjectId = Integer.parseInt(sub);
				%>
				<center>
				<form action="Exam.do" method="post" name="fm" onsubmit="return inputsValidations();" onreset="onResetClicked()">
					<input type="hidden" name="subjectID" value="<%= subjectId%>"/>
					<input type="hidden" name="CMD" value="create"/>
					<input type="hidden" name="examid" value="0"/>
					<input type="hidden" name="questionIdsSelected"/>
					<table>
						<tr><td>Exam Name: </td><td></td><td><input type="text" name="examName"/></td></tr>
						<%
							if(subjectId==0)
							{
								ArrayList<SubjectBean> subs = (ArrayList<SubjectBean>) session.getAttribute("Subjects");
								out.println("<tr><td>Subject Name: </td><td></td><td><select name='subjectName' onchange='javascript: changed()'>");
								out.println("<option value='0'>Select</option>");
								Iterator<SubjectBean> itr = subs.iterator(); 
								while(itr.hasNext()) 
								{
									SubjectBean sb = (SubjectBean)itr.next();	
									out.println("<option value='"+sb.getSubjectId()+"'>"+sb.getSubjectName()+"</option>");
								}
								out.println("</td></tr>");
							}
						%>
						<tr><td>Number of Questions: </td><td></td><td><input type="text" name="noOfQues" maxlength="2"/></td></tr>
						<tr><td>Select Questions: </td><td></td><td>Displaying below are question ids. Click on question to know more.</td></tr>
						<tr><td></td>
						<% 
							DatabaseOperations dbOperations = new DatabaseOperations();
							ArrayList<QuestionsBean> ques = dbOperations.getQuestions(subjectId);
							session.setAttribute("Questions",ques);
							if(ques.size()>0)
							{
								Iterator<QuestionsBean> itr = ques.iterator(); 
								while(itr.hasNext()) 
								{
									QuestionsBean qb = (QuestionsBean)itr.next();	
									out.println("<tr><td></td><td></td><td><input type='checkbox' name='quesions' value='"+qb.getQuestionId()+"'>"+
											"<a href='javascript:showQuestion("+qb.getQuestionId()+")'>"
											+qb.getQuestionId()+"</a></input></td></tr>");
								}
							}else
							{
								
							}
							
						
						%>
						
						<tr><td>Maximum Marks: </td><td></td><td><input type="text" name="maxMarks" maxlength="3"/></td></tr>
						<tr><td>Pass Marks: </td><td></td><td><input type="text" name="passMarks" maxlength="3"/></td></tr>
						<tr><td>Exam Fees: </td><td></td><td><input type="text" name="examFees" maxlength="5"/></td></tr>
						<tr><td>Duration: </td><td></td><td><input type="text" name="examDuration" maxlength="2"/></td></tr>
						<tr></tr>
						<tr><td></td><td><input type="submit" name="submit" value="Submit"/>
						<input type="reset" value="Reset"></td></tr>
					</table>
				</form>
				</center>
				<%
			}
		%>
		</div>
		
		<hr>
	
	</div>
</body>
</html>